<template>
  <div class="app-container">
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      ref="crud"
      @row-del="rowDel"
      v-model="form"
      @row-update="rowUpdate"
      @row-save="rowSave"
      :before-open="beforeOpen"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
    </avue-crud>
  </div>
</template>

<script>
// import {
//   getList,
//   remove,
//   update,
//   add,
//   getNotice,
// } from "";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      option: {
        selection: true,
        card: true,
        tip: false,
        index: true,
        indexLabel: "序号",
        addTitle: "添加用户信息",
        editTitle: "编辑用户信息",
        viewTitle: "添加用户信息",
        indexWidth: 80,
        delBtn: false,
        addBtn: true,
        editBtn: false,
        viewBtn: false,
        labelPosition: "right",
        labelSuffix: "：",
        searchLabelWidth: 140,
        column: [
          {
            prop: "title",
            label: "标题",
          },
        ],
      },
      data: [
        {
          releaseTime: "2016-05-02",
          title: "王小虎",
          content: "上海市普陀区金沙江路 1518 弄",
        },
        {
          releaseTime: "2016-05-04",
          title: "王小虎",
          content: "上海市普陀区金沙江路 1517 弄",
        },
        {
          releaseTime: "2016-05-01",
          title: "王小虎",
          content: "上海市普陀区金沙江路 1519 弄",
        },
      ],
    };
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getNotice(this.form.id).then((res) => {
          this.form = res.data;
        });
      }
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      // let values = {
      //   ...params,
      //   descs: "create_time",
      // };

      // this.loading = true;
      // getList(page.currentPage, page.pageSize, Object.assign(values, this.query)).then(res => {
      //   const data = res.data;
      //   this.page.total = data.total;
      //   this.data = data.records;
      this.loading = false;
      //   this.selectionClear();
      // });
    },
  },
};
</script>
